package com.example.statistics.common

import android.content.Context
import android.os.Bundle
import androidx.annotation.LayoutRes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.statistics.R
import kotlinx.android.synthetic.main.fragment_base.view.*
import kotlinx.android.synthetic.main.layout_empty_view.view.*

abstract class BaseFragment : androidx.fragment.app.Fragment() {

    lateinit var mParentView: View

    lateinit var mActivity: androidx.fragment.app.FragmentActivity

    @get:LayoutRes
    abstract val layoutRes: Int
    open var isInitDataSuccess: Boolean = false
        set(value) {
            if (value) {
                mParentView.include_empty_view.visibility = View.GONE
            } else {
                mParentView.include_empty_view.visibility = View.VISIBLE
            }
            field = value
        }

    val tv_refresh_reason
        get() = mParentView.tv_refresh_reason

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = context as androidx.fragment.app.FragmentActivity
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_base, container, false)
        val contentView = inflater.inflate(layoutRes, view.frame_layout, false)
        view.frame_layout.addView(contentView)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mParentView = view
    }

    class TabPageAdapter(fragmentManager: androidx.fragment.app.FragmentManager, val fragmentList: List<Pair<androidx.fragment.app.Fragment, String>>) : androidx.fragment.app.FragmentPagerAdapter(fragmentManager) {
        override fun getItem(p0: Int): androidx.fragment.app.Fragment {
            return fragmentList[p0].first
        }

        override fun getCount(): Int {
            return fragmentList.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return fragmentList[position].second
        }


    }

}